package com.monitor;

import com.monitor.bean.ConsumerComponent;
import com.monitor.common.TPMonitorUtil;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class ConsumerMain {

    public static void main(String[] args) throws IOException, InterruptedException {

        ExecutorService executorService = Executors.newFixedThreadPool(9);

        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConsumerConfiguration.class);
        context.start();
        ConsumerComponent service = context.getBean(ConsumerComponent.class);
        System.in.read();

        TPMonitorUtil.gatherVeryMinutes();

        new Thread(()->{
            while (true){
                try {
                    TimeUnit.SECONDS.sleep(5);
                    System.out.println("methodA  TP90:"+TPMonitorUtil.getTP(TPMonitorUtil.perSecondRunTimeQueueA,TPMonitorUtil.perMinuteQueueA,0.90));
                    System.out.println("methodB  TP90:"+TPMonitorUtil.getTP(TPMonitorUtil.perSecondRunTimeQueueB,TPMonitorUtil.perMinuteQueueB,0.90));
                    System.out.println("methodC  TP90:"+TPMonitorUtil.getTP(TPMonitorUtil.perSecondRunTimeQueueC,TPMonitorUtil.perMinuteQueueC,0.90));
                    System.out.println("methodA  TP99:"+TPMonitorUtil.getTP(TPMonitorUtil.perSecondRunTimeQueueA,TPMonitorUtil.perMinuteQueueA,0.99));
                    System.out.println("methodB  TP99:"+TPMonitorUtil.getTP(TPMonitorUtil.perSecondRunTimeQueueB,TPMonitorUtil.perMinuteQueueB,0.99));
                    System.out.println("methodC  TP99:"+TPMonitorUtil.getTP(TPMonitorUtil.perSecondRunTimeQueueC,TPMonitorUtil.perMinuteQueueC,0.99));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();

        executorService.submit(()->{
            while (true) {
                service.methodA();
            }
        });
        executorService.submit(()->{
            while (true) {
                service.methodB();
            }
        });
        executorService.submit(()->{
            while (true){
                service.methodC();
            }
        });
        executorService.submit(()->{
            while (true) {
                service.methodA();
            }
        });
        executorService.submit(()->{
            while (true) {
                service.methodB();
            }
        });
        executorService.submit(()->{
            while (true){
                service.methodC();
            }
        });

    }

    @Configuration
    @PropertySource("classpath:/dubbo-consumer.properties")
    @ComponentScan("com.monitor.bean")
    @EnableDubbo
    static class ConsumerConfiguration {

    }


}
